package xin.pms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xin.common.http.response.PageResponse;
import xin.common.http.response.Response;
import xin.pms.model.Menu;
import xin.pms.model.MenuNode;
import xin.pms.request.MenuPageRequest;
import xin.pms.request.MenuRequest;
import xin.pms.service.MenuService;

import java.util.List;

/**
 * <pre>
 * Menu Controller
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/7/28 16:34
 */
@RestController
@RequestMapping("pms/menu")
@Api(value = "xin-pms", description = "菜单管理", position = 3)
@Slf4j
public class MenuController {

    @Autowired
    private MenuService service;

    @ApiOperation(value = "获取菜单列表" , notes = "获取菜单列表",response = PageResponse.class )
    @GetMapping("pageList")
    public PageResponse getUserList(MenuPageRequest request){
        return service.findPageList(request);
    }

    @ApiOperation(value = "获取菜单" , notes = "获取菜单",response = Response.class )
    @GetMapping("/{menuId}")
    public Response<Menu> get(@PathVariable String menuId){
        Response<Menu> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.findOne(menuId));
        return response;
    }

    @ApiOperation(value = "添加菜单" , notes = "添加菜单",response = Response.class )
    @PostMapping("add")
    public Response put(@RequestBody MenuNode node){
        Response<Menu> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.save(node));
        return response;
    }

    @ApiOperation(value = "修改菜单(属性信息)" , notes = "只修改内容，属性不修改顺序",response = Response.class )
    @PostMapping("update")
    public Response update(@RequestBody MenuNode node){
        Response response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.update(node));
        return response;
    }


    @ApiOperation(value = "修改菜单(顺序)" , notes = "需要记录拖拽后的父菜单和前置菜单",response = Response.class )
    @PostMapping("updateParent")
    public Response updateParent(@RequestBody MenuRequest request){
        Response response = new Response();
        response.setSuccess(Boolean.TRUE);
        service.modifyParent(request);
        return response;
    }


    @ApiOperation(value = "删除菜单" , notes = "逻辑删除",response = Response.class )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", required = true, paramType = "query")
    })
    @DeleteMapping
    public Response logicalDel(String menuId){
        Response response = new Response();
        response.setSuccess(Boolean.TRUE);
        service.logicalDel(menuId);
        return response;
    }


    @ApiOperation(value = "通过父用户ID获取菜单" , notes = "通过父用户ID获取菜单",response = List.class )
    @GetMapping("byParent/{parentId}")
    public List<Menu> byParent(@PathVariable  String parentId){
        return service.getByParent(parentId);
    }

}
